<script setup lang="ts">
import type {HospitalLevelAndRegionArr, HospitalLevelAndRegionResponseData} from "@/api/home/type.ts";
import {reqHospitalLevelAndRegion} from '@/api/home/index.ts'
import {onMounted, ref} from "vue";

// 定义一个数组存储医院地区的数据
const regionArr = ref<HospitalLevelAndRegionArr>([])
// 控制地区高亮的响应式数据
let activeFlag = ref<string>('')
// 组件挂载完毕
onMounted(() => {
  getRegion()
})

// 获取医院地区数据
const getRegion = async () => {
  let result: HospitalLevelAndRegionResponseData = await reqHospitalLevelAndRegion('Beijin')
  // 存储医院等级数据
  if (result.code ===200) {
    regionArr.value = result.data
  }
}

// 点击地区的事件
const  changeRegion =(region:string)=> {
  // 高亮响应式数据存储leve数值
  activeFlag.value = region
  // 给父组件发送自定义事件:将医院地区参数回传给父组件
  $emit('getRegion',region)
}

// 定义自定义事件
let $emit = defineEmits(['getRegion'])
</script>

<template>
  <div class="region">
    <div class="content">
      <div class="left">地区:</div>
      <ul>
        <li :class="{active:activeFlag==''}" @click="changeRegion('')">全部</li>
        <li v-for="(item,index) in regionArr"
            :key="item.value"
            :class="{active:activeFlag==item.value}"
            @click="changeRegion(item.value)">{{item.name}}</li>
      </ul>
    </div>
  </div>
</template>

<style scoped lang="scss">
.region {
  color: #7f7f7f;
  margin-top: 10px;
  .content {
    display: flex;
    .left {
      margin-right: 10px;
      width: 36px;
    }
    ul {
      width: 80%;
      display: flex;
      flex-wrap: wrap;  // 一行放不下换行处理
      li {
        margin-right: 10px;
        margin-bottom: 10px;
        &.active {
          color: #55a6fe;
        }
      }
      li:hover{
        color: #55a6fe;
        cursor: pointer;
      }
    }
  }
}
</style>
